﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace MailTool.InfoControls
{
    public partial class Pagination : System.Web.UI.UserControl
    {
        private int _PageIndex;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindPager();//绑定分页数字
            }
        }
        //说明：
        //PageIndex：第几页
        //PageSize：每页多少条
        //Count：记录总数
        //BindName：绑定的方法名称，用于反射绑定数据
        public int PageIndex
        {
            get { if (_PageIndex == 0) { _PageIndex = 1; } return _PageIndex; }
            set { _PageIndex = value; }
        }
        private int _PageSize;
        public int PageSize
        {
            get { if (_PageSize == 0) { _PageSize = 1; } return _PageSize; }
            set { _PageSize = value; }
        }
        private int _Count;
        public int Count
        {
            get { return _Count; }
            set { _Count = value; }
        }
        private int _PageCount;
        private int PageCount
        {
            get
            {
                _PageCount = (Count % PageSize) == 0 ? Count / PageSize : Count / PageSize + 1;
                return _PageCount;
            }
            set { _PageCount = value; }
        }
        /// 
        /// 该方法名称修饰符为需为public
        /// 
        public string BindName
        {
            get { return hfBindName.Value; }
            set { hfBindName.Value = value; }
        }

        public void BindPager()
        {
            int _pagecount = (PageCount != 0 ? PageCount : 1);
            SetButtonEnable(_pagecount);//下面四个按钮可用状态设置
            this.lblTotal.Text = _pagecount.ToString();
            this.lblPageIndex.Text = PageIndex.ToString();
            this.lblPageSize.Text = PageSize.ToString();
            this.lblTotalRecord.Text = Count.ToString();
        }

        private void SetButtonEnable(int pageCount)
        {
            lbtnFirstLink.Enabled = PageIndex != 1;
            lbtnPrevPage.Enabled = PageIndex != 1;
            lbtnNextPage.Enabled = PageIndex != pageCount;
            lbtnLastPage.Enabled = PageIndex != pageCount;
            lbtnFirstLink.CommandArgument = "1";
            lbtnPrevPage.CommandArgument = (PageIndex - 1).ToString();
            lbtnNextPage.CommandArgument = (PageIndex + 1).ToString();
            lbtnLastPage.CommandArgument = pageCount.ToString();
        }
        protected void ClickEvent(object sender, EventArgs e)
        {
            ReBindData(((LinkButton)sender).CommandArgument.ToString());
        }

        private void ReBindData(string pageIndex)
        {
            PageIndex = int.Parse(pageIndex);
            InvokeParent();
            BindPager();
        }

        private void InvokeParent()
        {
            object obj = base.Parent is HtmlForm ? this.Page : this.Parent.Page;
            Type pageType = obj.GetType();
            MethodInfo mi = pageType.GetMethod(BindName);
            mi.Invoke(obj, null);
        }

        protected void BtnSubGoPage_Click(object sender, EventArgs e)
        {
            InvokeParent();
            string topage = this.TbxGoPage.Text;
            Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
            if (regex.IsMatch(topage))
            {
                int topageInt = int.Parse(topage);
                if (topageInt > PageCount)
                {
                    topage = PageCount.ToString();
                }
                ReBindData(topage);
            }
        }
    }
}